package cn.bytecollege.admin.controller; /**
 * @Auther：MR.P
 * @Date:2021/10/28 10 53
 * @Description:添加分类
 */

import cn.bytecollege.admin.dao.PaymentDao;
import cn.bytecollege.admin.service.PaymentService;
import cn.bytecollege.common.entity.Payment;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "PaymentSaveController", value = "/admin/paymentSave")
public class PaymentSaveController extends HttpServlet {
    private PaymentService paymentService = null;
    public PaymentSaveController(){
        paymentService = new PaymentService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/html/device/paymentSave.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = null;
        String paymentName = request.getParameter("paymentName");
        //通过支付名字查询支付方式
        Payment tempPayment = paymentService.findByPaymentName(paymentName);
        //判断是否存在相同支付方式
        if(tempPayment != null){
            msg = "该支付方式已存在";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/admin/html/device/paymentSave.jsp").forward(request,response);
            return;
        }
        //封装数据
        Payment payment = new Payment();
        payment.setPaymentName(paymentName);
        payment.setDeleteFlag(0);
        //保存支付方式
        paymentService.save(payment);
        msg = "添加成功";
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/admin/paymentList").forward(request,response);
    }
}
